package com.liangzhong.model.leagueMatch
{
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.NotifierEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.leagueMatch.vo.BaseMatchInfoVo;
	import com.liangzhong.model.leagueMatch.vo.ChampionMatchInfoVo;
	import com.liangzhong.model.leagueMatch.vo.TrialMatchInfoVo;
	import com.liangzhong.model.match.vo.MatchResultVo;
	import com.liangzhong.model.type.MatchType;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import lz.controls.Alert;

	/**
	 *联赛 
	 * @author lake
	 * 
	 */	
	public class LeagueMatchDataManager extends EventDispatcher
	{
		/**
		 * 比赛进程类型：
			1.	预选赛
			2.	甲级联赛
			3.	欧冠联赛
			4.	传奇挑战
		 */
		private var _ntype :int;
		/**
		 *俱乐部所在阵容：
		 * 1-第一阵容 ,2-第二阵容 
		 */		
		public var lineUpType 	:int = 1;
		/**
		 *联赛通过状态 
		 * 1 预选赛通过
		 * 2 甲级联赛通过
		 * 21 甲级联赛失败
		 * 3 冠军赛通过
		 * 31 冠军赛失败
		 * 4 传奇赛通过
		 * 41 传奇赛失败
		 */		
		public var passStatus	:int = 0;
		/**
		 *通过类型
		 *  MatchType
		 */		
		private var passType		:int = 0;
		/**
		 *预选赛信息 
		 */		
		public var trialMatchInfoVo :TrialMatchInfoVo = new TrialMatchInfoVo();
		/**
		 * 基础联赛信息
		 */		
		public var baseMatchInfoVo	:BaseMatchInfoVo = new BaseMatchInfoVo();
		/**
		 *冠军杯信息 
		 */		
		public var championMatchInfoVo :ChampionMatchInfoVo = new ChampionMatchInfoVo();
		/**
		 *预选赛挑战ids
		 * 六个国家 
		 * 1 英格兰 2 西班牙 3 法国 4 德国 5 意大利 6 中国
		 */		
		public var preMatchIDs :Array = [];
		/**
		 *甲级联赛挑战ids
		 * 六个国家 
		 * 1 英格兰 2 西班牙 3 法国 4 德国 5 意大利 6 中国
		 */		
		public var baseMatchIDs :Array = [];
		
		public function LeagueMatchDataManager()
		{
			//预选赛ids
			preMatchIDs.push([10038,10041,10048,10049,10050]);
			preMatchIDs.push([10065,10066,10067,10068,10069]);
			preMatchIDs.push([10058,10060,10061,10063,10064]);
			preMatchIDs.push([10052,10053,10054,10055,10056]);
			preMatchIDs.push([10070,10071,10072,10073,10074]);
			preMatchIDs.push([10031,10032,10033,10034,10035]);
			//甲级联赛ids
			baseMatchIDs.push([10021,10046,10045,10044,10043,10006,10003,10002,10005,10004,10001]);
			baseMatchIDs.push([10008,10024,10082,10081,10080,10079,10022,10014,10018,10009,10013]);
			baseMatchIDs.push([10087,10089,10091,10092,10093,10094,10095,10097,10098,10096,10020]);
			baseMatchIDs.push([10023,10090,10088,10086,10085,10047,10084,10083,10015,10011,10010]);
			baseMatchIDs.push([10078,10077,10076,10075,10062,10059,10057,10017,10012,10025,10007]);
			baseMatchIDs.push([10040,10039,10037,10036,10030,10029,10028,10027,10042,10026,10016]);
		}
		/**
		 *获取设置比赛类型 
		 * @param value
		 */		
		public function set ntype(value:int):void
		{
			_ntype = value;
			this.dispatchEvent(new Event(Event.CHANGE));
		}
		public function get ntype():int
		{
			return _ntype;
		}
		/**
		 *解析预选赛信息 
		 * @param data
		 * 
		 */		
		public function parseTrialMatchInfo(data:Object):void
		{
			trialMatchInfoVo.parse(data);
		}
		/**
		 *解析基础联赛信息 
		 * @param data
		 * 
		 */		
		public function parseBaseMatchInfo(data:Object):void
		{
			baseMatchInfoVo.parse(data);
		}
		/**
		 *解析冠军联赛信息 
		 * @param data
		 * 
		 */		
		public function parseChampionMatchInfo(data:Object):void
		{
			championMatchInfoVo.parse(data);
		}
		/**
		 *获取甲级联赛npcids 
		 * @return 
		 * 
		 */		
		public function getSelfBaseNpcids():Array
		{
			var country :int = AppData.getInstance().clubDatamanager.clubInfo.country;
			return baseMatchIDs[country-1];
		}
		/**
		 *设置下个状态 
		 * 
		 */		
		public function setNextType():void
		{
			ntype = passType;
		}
		/**
		 *更新比赛结果 
		 * @param mresultVo
		 * 
		 */		
		public function updateResult(mresultVo :MatchResultVo):void
		{
			var index :int;
			var country :int = AppData.getInstance().clubDatamanager.clubInfo.country;
			switch(mresultVo.ntype)
			{
				case MatchType.PRE_MATCH://预选赛
				{
					index = preMatchIDs[country-1].indexOf(mresultVo.gcid);
					trialMatchInfoVo.updateResult({index:index,hgoal:mresultVo.hgold,ggoal:mresultVo.ggold});
					if(trialMatchInfoVo.matchindex == 5)//预选赛通过
					{
						//Alert.showErrorTip("成功通过预选赛");
						ntype = MatchType.BASE_MATCH;
						passType = MatchType.BASE_MATCH;
						passStatus = 1;
						AppEventDispatcher.getInstance().dispatchEvt(NotifierEvent.MATCH_PRE_SUCCESS);
					}
					break;
				}
				case MatchType.BASE_MATCH://甲级联赛
				{
					index = baseMatchIDs[country-1].indexOf(mresultVo.gcid);
					baseMatchInfoVo.updateResult({npcid:mresultVo.gcid,index:index,hgoal:mresultVo.hgold,ggoal:mresultVo.ggold});
					if(baseMatchInfoVo.matchindex == 11 
						&& baseMatchInfoVo.win >= 9)//甲级联赛通过
					{
						//Alert.showErrorTip("成功通过甲级联赛");
						ntype = MatchType.CHAMPION_MATCH;
						passType = MatchType.CHAMPION_MATCH;
						passStatus = 2;
						AppEventDispatcher.getInstance().dispatchEvt(NotifierEvent.MATCH_BASE_SUCCESS,{retcode:0});
					}
					else
					{
						if(baseMatchInfoVo.matchindex == 11)
						{
							passStatus = 21;
							//Alert.showErrorTip("甲级联赛挑战失败");
							passType = MatchType.BASE_MATCH;
							AppEventDispatcher.getInstance().dispatchEvt(NotifierEvent.MATCH_BASE_SUCCESS,{retcode:1});
						}
					}
					break;
				}
				case MatchType.CHAMPION_MATCH://冠军赛
				{
					if(mresultVo.hgold > mresultVo.ggold)//晋级下一轮
					{
						AppEventDispatcher.getInstance().dispatchEvt(NotifierEvent.MATCH_CHAMPION_TRUN_SUCCESS,{retcode:0});
					}
					else
					{
						//Alert.showErrorTip("冠军联赛挑战失败");
						passStatus = 31;
						ntype = MatchType.BASE_MATCH;
						passType = MatchType.BASE_MATCH;
						AppEventDispatcher.getInstance().dispatchEvt(NotifierEvent.MATCH_CHAMPION_TRUN_SUCCESS,{retcode:1});
					}
					break;
				}
				case MatchType.LEGEND_MATCH://传奇
				{
					if(mresultVo.hgold > mresultVo.ggold)//晋级下一轮
					{
						passStatus = 4;
					}
					else
					{
						passStatus = 41;
						//Alert.showErrorTip("传奇挑战失败");
					}
					ntype = MatchType.BASE_MATCH
					passType = MatchType.BASE_MATCH;
					AppEventDispatcher.getInstance().dispatchEvt(NotifierEvent.MATCH_LEGEND_SUCCESS);
					break;
				}
			}
		}
	}
}